﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DNA.Models;
using System.IO;
using System.Xml;

namespace DNA.Controllers
{ 
    public class DiseaseController : Controller
    {
        private DNAAnalysisEntities db = new DNAAnalysisEntities();

        //
        // GET: /Disease/

        public ViewResult Index()
        {
            return View(db.Disease.ToList());
        }

        //
        // GET: /Disease/Details/5

        public ViewResult Details(int id)
        {
            Disease disease = db.Disease.Single(d => d.Id == id);
            return View(disease);
        }

        //
        // GET: /Disease/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Disease/Create

        [HttpPost]
        public ActionResult Create(Disease disease)
        {
            if(ModelState.IsValid)
            {
                db.Disease.AddObject(disease);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(disease);
        }

        //
        // GET: /Disease/Importar

        public ActionResult Importar()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Importar(Disease disease)
        {
            //Caminho do arquivo xml
            string arquivo = "";

            //Prepara a leitura do arquivo
            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase postedFile = Request.Files[fileName];

                postedFile.SaveAs(Server.MapPath("~/XMLs/") + Path.GetFileName(postedFile.FileName));

                arquivo = Server.MapPath("~/XMLs/") + Path.GetFileName(postedFile.FileName);
            }

            //Cria uma variavel do tipo xml
            XmlDocument xmlDoc = new XmlDocument();

            //Carrega o arquivo xml
            xmlDoc.Load(arquivo);

 
            //Percorre todos os nós do arquivo xml
            for (int i = 0; i < xmlDoc.DocumentElement.ChildNodes.Count; i++)
            {
                Disease dis = new Disease();
                //Preenche o objeto de doença com o nome da doença do XML
                dis.Name = xmlDoc.DocumentElement.ChildNodes[i].InnerText.ToString();
                db.Disease.AddObject(dis);
                db.SaveChanges();
            }



            return RedirectToAction("Index"); 
        }
        
        //
        // GET: /Disease/Edit/5
 
        public ActionResult Edit(int id)
        {
            Disease disease = db.Disease.Single(d => d.Id == id);
            return View(disease);
        }

        //
        // POST: /Disease/Edit/5

        [HttpPost]
        public ActionResult Edit(Disease disease)
        {
            if (ModelState.IsValid)
            {
                db.Disease.Attach(disease);
                db.ObjectStateManager.ChangeObjectState(disease, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(disease);
        }

        //
        // GET: /Disease/Delete/5
 
        public ActionResult Delete(int id)
        {
            Disease disease = db.Disease.Single(d => d.Id == id);
            return View(disease);
        }

        //
        // POST: /Disease/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Disease disease = db.Disease.Single(d => d.Id == id);
            db.Disease.DeleteObject(disease);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


    }
}